<template >
  <div class="index-collapse">
    <template v-for="(v,i) in quesitionArr">
      <div :key="i" class="item" @click="expand(i)">
        <div class="question-box">
          <p class="question">{{ v.question }}</p>
          <div class="icon" :class="{'active': clickArr.includes(i)}"></div>
        </div>
        <div class="answer" ref="answerArr">
          <p>{{ v.answer }}</p>
        </div>
      </div>
    </template>
  </div>
</template>

<script>
export default {

  props: {
    quesitionArr: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      clickArr: []
    }
  },
  methods: {
    expand (i) {
      let cur = this.$refs.answerArr[i]
      if (this.clickArr.includes(i)) {
        this.clickArr.splice(this.clickArr.indexOf(i), 1)
        cur.style.height = "0"
      } else {
        cur.style.height = cur.scrollHeight + "px"
        this.clickArr.push(i)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.index-collapse {
  width: 100%;
  display: flex;
  flex-direction: column;
  background-color: #fff;
  padding: rem(16);

  .item {
    width: 100%;
    display: flex;
    flex-direction: column;
    background-color: #fff;
    border-bottom: rem(1) solid #ebeef5;

    .question-box {
      @include between;
      width: 100%;
      box-sizing: border-box;
      align-items: center;
      transition: border-bottom-color 0.3s;

      .question {
        font-size: rem(14);
        line-height: rem(48);
        font-weight: bold;
        white-space: nowrap;
        color: inherit;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      .icon {
        width: rem(8);
        height: rem(8);
        border-bottom: rem(1) solid rgb(187, 187, 187);
        border-left: rem(1) solid rgb(187, 187, 187);
        transform: rotate(-45deg);
        transition-property: transform, -webkit-transform;
        transition-duration: 0.3s;
        transition-timing-function: ease;
      }
      .active {
        transform: rotate(-225deg);
      }
    }

    .answer {
      font-size: rem(13);
      color: rgba(0, 0, 0, 0.5);
      overflow: hidden;
      height: 0;
      transition: height 300ms ease;
      will-change: height;

      p {
        padding: rem(16) 0;
      }
    }
  }
}
</style>


